package com.ezlearning.platform.services.core.impl;

import com.ezlearning.platform.dto.StudentsDto;
import com.ezlearning.platform.model.Students;
import com.ezlearning.platform.repositories.StudentsRepository;
import com.ezlearning.platform.services.core.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentsService implements GenericService<Students, Long> {

    private StudentsRepository studentsRepository;

    @Autowired
    public StudentsService(StudentsRepository studentsRepository) {
        this.studentsRepository = studentsRepository;
    }

    public void create(StudentsDto studentsDto) {
        String nombre = studentsDto.getNombre();
        String apellido = studentsDto.getApellido();
        String correo = studentsDto.getCorreo();
        String descripcion = studentsDto.getDescripcion();
        String imgurl = studentsDto.getImgurl();
        Students students = new Students(nombre, apellido, correo, descripcion, imgurl);

        studentsRepository.save(students);
    }

    @Override
    public void create(Students students) {
        studentsRepository.save(students);
    }

    @Override
    public void update(Students students) {
        Students currentStudents = studentsRepository.findById(students.getId_students()).get();

        currentStudents.setNomStudents(students.getNomStudents());
        currentStudents.setApeStudents(students.getApeStudents());
        currentStudents.setCorreoStudents(students.getCorreoStudents());
        currentStudents.setDescStudents(students.getDescStudents());
        currentStudents.setImgurl(students.getImgurl());

        studentsRepository.save(currentStudents);
    }

    public void patch(Students students) {
        Students current = studentsRepository.findById(students.getId_students()).get();
        current.setDetalleStudents(students.getDetalleStudents());
        studentsRepository.save(current);
    }

    @Override
    public void delete(Students students) {
        studentsRepository.delete(students);
    }

    @Override
    public List<Students> getAll() {
        return studentsRepository.findAll();
    }

    @Override
    public Students findById(Long id) {
        return studentsRepository.findById(id).orElse(null);
    }
}